package cn.ycc1.baseset.basics;

/**
 * 运算符
 * @author ycc
 * @date 2025/3/3
 * All binary operators except for the assignment operators are evaluated from left to right;
 * assignment operators are evaluated right to left.
 *
 * Operators	Precedence
 * postfix	expr++ expr--
 * unary	++expr --expr +expr -expr ~ !
 * multiplicative	* / %
 * additive	+ -
 * shift	<< >> >>>
 * relational	< > <= >= instanceof
 * equality	== !=
 * bitwise AND	&
 * bitwise exclusive OR	^
 * bitwise inclusive OR	|
 * logical AND	&&
 * logical OR	||
 * ternary	? :
 * assignment	= += -= *= /= %= &= ^= |= <<= >>= >>>=
 *
 * Summary of Operators
 * Simple Assignment Operator
 * Operator	Description
 * =	Simple assignment operator
 *
 * Arithmetic Operators
 * Operator	Description
 * +	Additive operator (also used for String concatenation)
 * -	Subtraction operator
 * *	Multiplication operator
 * /	Division operator
 * %	Remainder operator
 *
 * Unary Operators
 * Operator	Description
 * +	Unary plus operator; indicates positive value (numbers are positive without this, however)
 * -	Unary minus operator; negates an expression
 * ++	Increment operator; increments a value by 1
 * --	Decrement operator; decrements a value by 1
 * !	Logical complement operator; inverts the value of a boolean
 *
 * Equality and Relational Operators
 * Operator	Description
 * ==	Equal to
 * !=	Not equal to
 * >	Greater than
 * >=	Greater than or equal to
 * <	Less than
 * <=	Less than or equal to
 *
 * Conditional Operators
 * Operator	Description
 * &&	Conditional-AND
 * ||	Conditional-OR
 * ?:	Ternary (shorthand for if-then-else statement)
 *
 * Type Comparison Operator
 * Operator	Description
 * instanceof	Compares an object to a specified type
 *
 * Bitwise and Bit Shift Operators
 * Operator	Description
 * ~	Unary bitwise complement
 * <<	Signed left shift
 * >>	Signed right shift
 * >>>	Unsigned right shift
 * &	Bitwise AND
 * ^	Bitwise exclusive OR
 * |	Bitwise inclusive OR
 */
public class Operators {
    /**
     * The Unary Operators
     */
    public static void unaryDemo() {
        int result = +1;
        // result is now 1
        System.out.println(result);

        result--;
        // result is now 0
        System.out.println(result);

        result++;
        // result is now 1
        System.out.println(result);

        result = -result;
        // result is now -1
        System.out.println(result);

        boolean success = false;
        // false
        System.out.println(success);
        // true
        System.out.println(!success);
    }

    public static void prePostDemo() {
        int i = 3;
        i++;
        // prints 4
        System.out.println(i);
        ++i;
        // prints 5
        System.out.println(i);
        // prints 6
        System.out.println(++i);
        // prints 6
        System.out.println(i++);
        // prints 7
        System.out.println(i);
    }

    /**
     * Operator	Description
     * ==	equal to
     * !=	not equal to
     * >	greater than
     * >=	greater than or equal to
     * <	less than
     * <=	less than or equal to
     */
    public static void comparisonDemo() {
        int value1 = 1;
        int value2 = 2;
        if(value1 == value2)
            System.out.println("value1 == value2");
        if(value1 != value2)
            System.out.println("value1 != value2");
        if(value1 > value2)
            System.out.println("value1 > value2");
        if(value1 < value2)
            System.out.println("value1 < value2");
        if(value1 <= value2)
            System.out.println("value1 <= value2");
    }

    public static void main (String[] args) {

        int result = 1 + 2;
        // result is now 3
        System.out.println("1 + 2 = " + result);
        int original_result = result;

        result = result - 1;
        // result is now 2
        System.out.println(original_result + " - 1 = " + result);
        original_result = result;

        result = result * 2;
        // result is now 4
        System.out.println(original_result + " * 2 = " + result);
        original_result = result;

        result = result / 2;
        // result is now 2
        System.out.println(original_result + " / 2 = " + result);
        original_result = result;

        result = result + 8;
        // result is now 10
        System.out.println(original_result + " + 8 = " + result);
        original_result = result;

        result = result % 7;
        // result is now 3
        System.out.println(original_result + " % 7 = " + result);

        unaryDemo();
    }

    /**
     * The Conditional Operators
     * Operator	Description
     * &&	Conditional-AND
     * ||	Conditional-OR
     */
    class ConditionalDemo1 {

        public static void main(String[] args){
            int value1 = 1;
            int value2 = 2;
            if ((value1 == 1) && (value2 == 2))
                System.out.println("value1 is 1 AND value2 is 2");
            if ((value1 == 1) || (value2 == 1))
                System.out.println("value1 is 1 OR value2 is 1");
        }
    }

    /**
     * The Type Comparison Operator Instanceof
     */
    class InstanceofDemo {
        public void main(String[] args) {

            Parent obj1 = new Parent();
            Parent obj2 = new Child();

            System.out.println("obj1 instanceof Parent: "
                    + (obj1 instanceof Parent));
            System.out.println("obj1 instanceof Child: "
                    + (obj1 instanceof Child));
            System.out.println("obj1 instanceof MyInterface: "
                    + (obj1 instanceof MyInterface));
            System.out.println("obj2 instanceof Parent: "
                    + (obj2 instanceof Parent));
            System.out.println("obj2 instanceof Child: "
                    + (obj2 instanceof Child));
            System.out.println("obj2 instanceof MyInterface: "
                    + (obj2 instanceof MyInterface));
        }
    }

    class Parent {}
    class Child extends Parent implements MyInterface {}
    interface MyInterface {}

    /**
     * Bitwise and Bit Shift Operators
     */
    class BitDemo {
        public static void main(String[] args) {
            int bitmask = 0x000F;
            int val = 0x2222;
            // prints "2"
            System.out.println(val & bitmask);
        }
    }

}
